<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="关联子表的表名" prop="subTableName">
        <el-input
          v-model="queryParams.subTableName"
          placeholder="请输入关联子表的表名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子表关联的外键名" prop="subTableFkName">
        <el-input
          v-model="queryParams.subTableFkName"
          placeholder="请输入子表关联的外键名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实体类名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入实体类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用的模板" prop="tplCategory">
        <el-input
          v-model="queryParams.tplCategory"
          placeholder="请输入使用的模板"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成包路径" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入生成包路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成模块名" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入生成模块名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成业务名" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入生成业务名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成功能名" prop="functionName">
        <el-input
          v-model="queryParams.functionName"
          placeholder="请输入生成功能名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成功能作者" prop="functionAuthor">
        <el-input
          v-model="queryParams.functionAuthor"
          placeholder="请输入生成功能作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成代码方式" prop="genType">
        <el-select v-model="queryParams.genType" placeholder="请选择生成代码方式" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="生成路径" prop="genPath">
        <el-input
          v-model="queryParams.genPath"
          placeholder="请输入生成路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:table:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="openImportTable"
          v-hasPermi="['system:table:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:table:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:table:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:table:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleWord"
          v-hasPermi="['system:table:word']"
        >生成word</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="tableId" />
      <el-table-column label="表名称" align="center" prop="tableName" />
      <el-table-column label="表描述" align="center" prop="tableComment" />
      <!--<el-table-column label="关联子表的表名" align="center" prop="subTableName" />
      <el-table-column label="子表关联的外键名" align="center" prop="subTableFkName" />-->
      <el-table-column label="实体类名称" align="center" prop="className" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <!--<el-table-column label="使用的模板" align="center" prop="tplCategory" />
      <el-table-column label="生成包路径" align="center" prop="packageName" />
      <el-table-column label="生成模块名" align="center" prop="moduleName" />
      <el-table-column label="生成业务名" align="center" prop="businessName" />
      <el-table-column label="生成功能名" align="center" prop="functionName" />
      <el-table-column label="生成功能作者" align="center" prop="functionAuthor" />
      <el-table-column label="生成代码方式" align="center" prop="genType" />
      <el-table-column label="生成路径" align="center" prop="genPath" />
      <el-table-column label="其它生成选项" align="center" prop="options" />-->
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:table:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="ColumnIndex"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:table:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代码生成业务对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--&lt;!&ndash;        <el-form-item label="表名称" prop="tableName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.tableName" placeholder="请输入表名称" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="表描述" prop="tableComment">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.tableComment" type="textarea" placeholder="请输入内容" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="关联子表的表名" prop="subTableName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.subTableName" placeholder="请输入关联子表的表名" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="子表关联的外键名" prop="subTableFkName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.subTableFkName" placeholder="请输入子表关联的外键名" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="实体类名称" prop="className">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.className" placeholder="请输入实体类名称" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="使用的模板" prop="tplCategory">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.tplCategory" placeholder="请输入使用的模板" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成包路径" prop="packageName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.packageName" placeholder="请输入生成包路径" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成模块名" prop="moduleName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.moduleName" placeholder="请输入生成模块名" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成业务名" prop="businessName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.businessName" placeholder="请输入生成业务名" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成功能名" prop="functionName">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.functionName" placeholder="请输入生成功能名" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成功能作者" prop="functionAuthor">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.functionAuthor" placeholder="请输入生成功能作者" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成代码方式" prop="genType">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-select v-model="form.genType" placeholder="请选择生成代码方式">&ndash;&gt;-->
<!--&lt;!&ndash;            <el-option label="请选择字典生成" value="" />&ndash;&gt;-->
<!--&lt;!&ndash;          </el-select>&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="生成路径" prop="genPath">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.genPath" placeholder="请输入生成路径" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="其它生成选项" prop="options">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.options" type="textarea" placeholder="请输入内容" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-form-item label="备注" prop="remark">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->
<!--        <el-form-item label="归属表编号" prop="tableId">-->
<!--          <el-input v-model="form.tableId" placeholder="请输入归属表编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="列名称" prop="columnName">-->
<!--          <el-input v-model="form.columnName" placeholder="请输入列名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="列描述" prop="columnComment">-->
<!--          <el-input v-model="form.columnComment" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="列类型" prop="columnType">-->
<!--          <el-select v-model="form.columnType" placeholder="请选择列类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="JAVA类型" prop="javaType">-->
<!--          <el-select v-model="form.javaType" placeholder="请选择JAVA类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="JAVA字段名" prop="javaField">-->
<!--          <el-input v-model="form.javaField" placeholder="请输入JAVA字段名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否主键" prop="isPk">-->
<!--          <el-input v-model="form.isPk" placeholder="请输入是否主键" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否自增" prop="isIncrement">-->
<!--          <el-input v-model="form.isIncrement" placeholder="请输入是否自增" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否必填" prop="isRequired">-->
<!--          <el-input v-model="form.isRequired" placeholder="请输入是否必填" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否为插入字段" prop="isInsert">-->
<!--          <el-input v-model="form.isInsert" placeholder="请输入是否为插入字段" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否编辑字段" prop="isEdit">-->
<!--          <el-input v-model="form.isEdit" placeholder="请输入是否编辑字段" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否列表字段" prop="isList">-->
<!--          <el-input v-model="form.isList" placeholder="请输入是否列表字段" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否查询字段" prop="isQuery">-->
<!--          <el-input v-model="form.isQuery" placeholder="请输入是否查询字段" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="查询方式" prop="queryType">-->
<!--          <el-select v-model="form.queryType" placeholder="请选择查询方式">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="显示类型" prop="htmlType">-->
<!--          <el-select v-model="form.htmlType" placeholder="请选择显示类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="字典类型" prop="dictType">-->
<!--          <el-select v-model="form.dictType" placeholder="请选择字典类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排序" prop="sort">-->
<!--          <el-input v-model="form.sort" placeholder="请输入排序" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
    <import-table ref="import" @ok="handleQuery" />
  </div>
</template>

<script>
import { getListTable,listTable, getTable, delTable, addTable, updateTable, exportTable,wordTable } from "@/api/system/table";
import importTable from "./importTable";
export default {
  name: "Table",
  components: { importTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代码生成业务表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableIds:[],
        tableName: null,
        tableComment: null,
        subTableName: null,
        subTableFkName: null,
        className: null,
        tplCategory: null,
        packageName: null,
        moduleName: null,
        businessName: null,
        functionName: null,
        functionAuthor: null,
        genType: null,
        genPath: null,
        options: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListTable();
  },
  methods: {

    /** 查询代码生成业务列表 */
    getList() {
      this.loading = true;
      listTable(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListTable() {
      this.loading = true;
      listTable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tableId: null,
        tableName: null,
        tableComment: null,
        subTableName: null,
        subTableFkName: null,
        className: null,
        tplCategory: null,
        packageName: null,
        moduleName: null,
        businessName: null,
        functionName: null,
        functionAuthor: null,
        genType: null,
        genPath: null,
        options: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tableId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代码生成业务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tableId = row.tableId || this.ids
      this.$router.push({ path: '/system/table-edit/index', query: { tableId: tableId, pageNum: this.queryParams.pageNum } });
      getTable(tableId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代码生成业务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tableId != null) {
            updateTable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tableIds = row.tableId || this.ids;
      this.$modal.confirm('是否确认删除代码生成业务编号为"' + tableIds + '"的数据项？').then(function() {
        return delTable(tableIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有代码生成业务数据项？').then(() => {
        this.exportLoading = true;
        return exportTable(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 生成word按钮操作 */
    handleWord(row) {
      this.queryParams.tableIds = row.tableId || this.ids;
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有代码生成业务数据项？').then(() => {
        this.exportLoading = true;
        return wordTable(queryParams.tableIds);
      }).then(response => {
        this.$modal.msgSuccess("生成成功");
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 打开导入表弹窗 */
    openImportTable() {
      this.$refs.import.show();
    }
  }
};
</script>
